package com.slipper.groovy.util;

import com.slipper.PaasContext;
import com.slipper.codeless.resolve.query.Condition;
import com.slipper.codeless.service.CodelessService;
import com.slipper.constants.Constants;
import com.slipper.util.Tool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DataModelUtil {
    public static Map<String, Object> queryById(String code, String id) {
        if (Tool.isBlank(id)) return null;
        return PaasContext.getBean(CodelessService.class).queryByCode(code, id);
    }

    public static List<Map<String, Object>> queryList(String code, Map<String, Object> dataMap) {
        return queryList(code, dataMap, null);
    }

    public static List<Map<String, Object>> queryList(
            String code,
            Map<String, Object> dataMap,
            Map<String, String> orderMap) {
        List<List<Object>> inputParamList = new ArrayList<>();
        dataMap.forEach((key, value) -> {
            List<Object> paramList = new ArrayList<>();
            paramList.add(key);
            paramList.add(Condition.MATCH_MODE_EQU);
            paramList.add(value);
            inputParamList.add(paramList);
        });

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("inputParamList", inputParamList);
        return PaasContext.getBean(CodelessService.class).queryListByCode(code, paramMap, orderMap);
    }

    public static List<Map<String, Object>> queryList(String code, List<List<Object>> paramList) {
        return queryList(code, paramList, null);
    }

    public static List<Map<String, Object>> queryList(
            String code,
            List<List<Object>> paramList,
            Map<String, String> orderMap) {
        List<List<Object>> inputParamList = new ArrayList<>();
        paramList.forEach(param -> {
            if (param.size() < 2) return;
            List<Object> params = new ArrayList<>();
            params.add(String.valueOf(param.get(0)));
            params.add(param.size() >= 3 ? param.get(2) : Condition.MATCH_MODE_EQU);
            params.add(param.get(1));
            inputParamList.add(params);
        });

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("inputParamList", inputParamList);

        return PaasContext.getBean(CodelessService.class).queryListByCode(code, paramMap, orderMap);
    }

    public static List<Map<String, Object>> queryList(
            String code,
            int pageNumber,
            int pageSize,
            Map<String, Object> dataMap) {
        return queryList(code, pageNumber, pageSize, dataMap, null);
    }

    public static List<Map<String, Object>> queryList(
            String code,
            int pageNumber,
            int pageSize,
            Map<String, Object> dataMap,
            Map<String, String> orderByMap) {
        List<List<Object>> inputParamList = new ArrayList<>();

        dataMap.forEach((key, value) -> {
            List<Object> paramList = new ArrayList<>();
            paramList.add(key);
            paramList.add(Condition.MATCH_MODE_EQU);
            paramList.add(value);
            inputParamList.add(paramList);
        });

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("inputParamList", inputParamList);
        paramMap.put("pageNumber", pageNumber);
        paramMap.put("pageSize", pageSize);
        return PaasContext.getBean(CodelessService.class).queryPageByCode(code, paramMap, orderByMap).getList();
    }

    public static int insert(String code, Map<String, Object> paramMap) {
        return insert(code, paramMap, true);
    }

    public static int insert(String code, Map<String, Object> paramMap, boolean trigger) {
        return PaasContext.getBean(CodelessService.class).insert(code, Constants.getCurrentUser(false), paramMap, trigger);
    }

    public static int update(String code, Map<String, Object> paramMap) {
        return update(code, paramMap, true);
    }

    public static int update(String code, Map<String, Object> paramMap, boolean trigger) {
        return PaasContext.getBean(CodelessService.class).update(code, Constants.getCurrentUser(false), paramMap, trigger);
    }

    public static int batchInsert(String code, List<Map<String, Object>> dataList) {
        return batchInsert(code, dataList, true);
    }

    public static int batchInsert(String code, List<Map<String, Object>> dataList, boolean trigger) {
        if (dataList == null || dataList.isEmpty()) return 0;
        return PaasContext.getBean(CodelessService.class).batchInsert(code, Constants.getCurrentUser(false), dataList, trigger);
    }

    public static int batchUpdate(String code, List<Map<String, Object>> dataList) {
        return batchUpdate(code, dataList, true);
    }

    public static int batchUpdate(String code, List<Map<String, Object>> dataList, boolean trigger) {
        if (dataList == null || dataList.isEmpty()) return 0;
        return PaasContext.getBean(CodelessService.class).batchUpdate(code, Constants.getCurrentUser(false), dataList, trigger);
    }

    public static int deleteById(String code, String id) {
        return PaasContext.getBean(CodelessService.class).deleteById(code, id);
    }

    public static int batchDelete(String code, List<Object> idList) {
        return batchDelete(code, idList, true);
    }

    public static int batchDelete(String code, List<Object> idList, boolean trigger) {
        if (idList == null || idList.isEmpty()) return 0;
        return PaasContext.getBean(CodelessService.class).batchDelete(code, idList, trigger);
    }
}